package com.jcrj.dao.imp;

import com.jcrj.dao.AbstractBaseDao;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class AbstractBaseDaoImp implements AbstractBaseDao {
	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return this.hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public List findByHql(String hql) {
		return this.hibernateTemplate.find(hql);
	}

	public List findBySql(final String sql) {
		return (List) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				return query.list();
			}
		});
	}

	public List findByPageSql(final String sql, final int currentPage) {
		return (List) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				query.setFirstResult((currentPage - 1) * 10);
				query.setMaxResults(10);
				return query.list();
			}
		});
	}

	public Long countByPageSql(final String sql) {
		return (Long) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				return query.uniqueResult();
			}
		});
	}

	public Object getObject(Object obj, int id) {
		return getHibernateTemplate().get(obj.getClass(), Integer.valueOf(id));
	}

	public List findByPage(final String hql, final int currentPage) {
		return (List) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult((currentPage - 1) * 10);
				query.setMaxResults(10);
				return query.list();
			}
		});
	}

	public List findByPageBook(final String hql, final int currentPage, final int pageCount) {
		return (List) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult((currentPage - 1) * pageCount);
				query.setMaxResults(pageCount);
				return query.list();
			}
		});
	}

	public Long countTotalByHql(final String hql) {
		return (Long) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				return query.uniqueResult();
			}
		});
	}

	public void executeHql(final String hql) {
		this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				return Integer.valueOf(query.executeUpdate());
			}
		});
	}

	public List findListByName(String hql, String[] name, Object[] obj) {
		return getHibernateTemplate().findByNamedParam(hql, name, obj);
	}

	public List findListByPage(final String hql, final List list, final int currentPage) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setParameterList("list", list);
				query.setFirstResult((currentPage - 1) * 10);
				query.setMaxResults(10);
				return query.list();
			}
		});
	}

	public Long findListCount(final String hql, final List list) {
		return (Long) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setParameterList("list", list);
				return query.uniqueResult();
			}
		});
	}

	public List findListByHqlTop(final String hql, final int count) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(0);
				query.setMaxResults(count);
				return query.list();
			}
		});
	}

	public List findListBySqlTop(final String sql, final int count) {
		return (List) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				query.setFirstResult(0);
				query.setMaxResults(count);
				return query.list();
			}
		});
	}

	public void executeSql(final String sql) {
		this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				return Integer.valueOf(query.executeUpdate());
			}
		});
	}

	public Object getObject(Object obj, String id) {
		return getHibernateTemplate().get(obj.getClass(), id);
	}

	public BigDecimal countBySql(final String sql) {
		return (BigDecimal) this.hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(sql);
				return query.uniqueResult();
			}
		});
	}
}